package main

import (
	"fmt"
	"os"
	codecs "sanguo/codec/cs"
	"sanguo/node/client/dispatcher"
	"sanguo/node/client/login"
	cs_msg "sanguo/protocol/cs/message"

	"github.com/golang/protobuf/proto"
	"github.com/sniperHW/kendynet"
)

func main() {

	if len(os.Args) < 3 {
		fmt.Printf("usage selectRole userID addr\n")
		return
	}

	userID := os.Args[1]
	addr := os.Args[2]

	login.Login(userID, addr, func(session kendynet.StreamSession, d *dispatcher.Dispatcher, msg *codecs.Message, err error) {
		if nil != err {
			fmt.Println(err)
		} else {
			gameLoginToC := msg.GetData().(*cs_msg.GameLoginToC)
			fmt.Println("RoleList", gameLoginToC)
			if gameLoginToC.GetCode() == cs_msg.EnumType_OK {
				var roleIDMap map[int]uint64 = make(map[int]uint64)

				req := &cs_msg.RoleListToS{}
				session.Send(codecs.NewMessage(uint16(0), req))

				d.Register(&cs_msg.RoleMissionToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleMissionToC)
					fmt.Println("roleMission", resp)

				})
				d.Register(&cs_msg.RoleAttrToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleAttrToC)
					fmt.Println("RoleAttrToC", resp)

				})
				d.Register(&cs_msg.RoleBattleAttrToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleBattleAttrToC)
					fmt.Println("RoleBattleAttrToC", resp)

				})
				d.Register(&cs_msg.RoleItemToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleItemToC)
					fmt.Println("RoleItemToC", resp)

				})

				d.Register(&cs_msg.RoleSoulToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleSoulToC)
					fmt.Println("RoleSoulToC", resp)

				})

				d.Register(&cs_msg.RoleListToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleListToC)
					fmt.Println(resp.GetErrcode())
					for k, v := range resp.RoleList {
						fmt.Println(k, v)
						roleIDMap[k] = v.GetID()
						fmt.Println(k, roleIDMap[k])
					}

					if resp.GetErrcode() == cs_msg.EnumType_OK {
						go func() {
							fmt.Printf("选角编号:")
							var id int
							fmt.Scan(&id)
							req2 := &cs_msg.SelectRoleToS{
								ID: proto.Uint64(roleIDMap[id]),
							}
							session.Send(codecs.NewMessage(uint16(0), req2))
							d.Register(&cs_msg.SelectRoleToC{}, false, func(args []interface{}) {
								resp2 := args[1].(*codecs.Message).GetData().(*cs_msg.SelectRoleToC)
								fmt.Println(resp2)
								for _, v := range resp2.Souls {
									if v.GetIsEquip() {
										fmt.Println("soul-----", v)
									}
								}
							})
						}()

					}

				})

			}
		}
	})

	sigStop := make(chan bool)
	_, _ = <-sigStop
}
